package com.jiantong.lawEnforcing.impl.service;

import com.google.common.base.Throwables;
import com.jiantong.lawEnforcing.impl.dao.LegalProvisionsDao;
import com.jiantong.lawEnforcing.model.LegalProvisions;
import com.jiantong.lawEnforcing.service.LegalProvisionsService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 法律条文
 * Created by mz03 on 2017/8/17.
 */
@Slf4j
@Service
@RpcProvider
public class LegalProvisionsServiceImpl implements LegalProvisionsService {
    private final LegalProvisionsDao legalProvisionsDao;

    @Autowired
    public LegalProvisionsServiceImpl(LegalProvisionsDao legalProvisionsDao) {
        this.legalProvisionsDao = legalProvisionsDao;
    }

    /**
     * id 查找
     *
     * @param id
     * @return legalProvisions
     */
    @Override
    public Response<LegalProvisions> finById(Long id) {
        try {
            LegalProvisions legalProvisions = legalProvisionsDao.findById(id);
            if (legalProvisions != null) {
                return Response.ok(legalProvisions);
            } else {
                log.error("legalProvisions (id={}) is not found", id);
                return Response.fail("legalProvisions.not.found");
            }
        }catch (Exception e){
            log.error("failed to find legalProvisions(id={}),cause:{}",id, Throwables.getStackTraceAsString(e));
            return Response.fail("legalProvisions.find.fail");
        }
    }

    /**
     * 创建
     *
     * @param legalProvisions 单据对象
     * @return 主键 id
     */
    @Override
    public Response<Long> create(LegalProvisions legalProvisions) {
        try {
            legalProvisionsDao.create(legalProvisions);
            return Response.ok(legalProvisions.getId());
        }catch (Exception e) {
            log.error("failed.to.create.legalProvisions:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("create.legalProvisions.failed");
        }
    }

    /**
     * 更新
     *
     * @param legalProvisions 单据对象
     * @return true or false
     */
    @Override
    public Response<Boolean> update(LegalProvisions legalProvisions) {
        try{
            return Response.ok(legalProvisionsDao.update(legalProvisions));
        }catch (Exception e){
            log.error("failed.to.update.legalProvisions:{}",Throwables.getStackTraceAsString(e));
            return Response.fail("update.legalProvisions.failed");
        }
    }
}
